package com.ruoyi.web.controller.comlbs;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.BigDataConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BigDataException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.BigDataResponse;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.system.domain.AlarmHandleRecord;
import com.ruoyi.system.domain.request.alarmhandlerecord.GetAlarmVideoParam;
import com.ruoyi.system.domain.request.alarmhandlerecord.HandleAlarmParam;
import com.ruoyi.system.domain.request.dept.CompanyExistNameParam;
import com.ruoyi.system.service.IAlarmHandleRecordService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报警记录Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/alarmHandleRecord")
public class AlarmHandleRecordController extends BaseController {

    @Autowired
    private IAlarmHandleRecordService alarmHandleRecordService;
    @Autowired
    private BigDataConfig bigDataConfig;

    @ApiOperation("报警记录列表")
    @ApiOperationSupport(includeParameters = {"alarmType","numberPlate","alarmStartTime","alarmEndTime"})
    @GetMapping("/list")
    public TableDataInfo list(AlarmHandleRecord alarmHandleRecord) {
        startPage();
        List<AlarmHandleRecord> list = alarmHandleRecordService.selectList(alarmHandleRecord);
        return getDataTable(list);
    }

    @ApiOperation("报警记录处理详情")
    @GetMapping(value = "/{orderSendRecordId}")
    public AjaxResult getInfo(@PathVariable Long orderSendRecordId) {
        return success(alarmHandleRecordService.getDetail(orderSendRecordId));
    }

    @ApiOperation("新增报警处理记录")
    @Log(title = "报警处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<AlarmHandleRecord> list) {
        return toAjax(alarmHandleRecordService.saveBatch(list));
    }

    @ApiOperation("处理报警数据")
    @Log(title = "处理报警记录", businessType = BusinessType.INSERT)
    @PostMapping("/handleAlarm")
    public AjaxResult handleAlarm(@RequestBody List<HandleAlarmParam> params) {
        List<AlarmHandleRecord> resultList = params.stream().map(item -> new AlarmHandleRecord()
            .setRemark(item.getRemark())
            .toExtentEntity(AlarmHandleRecord.class)
            .setAlarmId(item.getId())
            .setDvo(item.getDvo())
            .setNumberPlate(item.getNumberPlate())
            .setCompanyId(item.getCompanyId())
            .setCompanyName(item.getCompanyName())
            .setAlarmType(item.getAlarmName())
            .setAlarmStartTime(item.getAdt())
            .setAlarmEndTime(item.getAdt())
            .setAlarmAddress(item.getAddress())
            .setOrderSendRecordId(item.getOrderRecordId())
            .setHandleCode(item.getHandleCode())
            .setHandleName(getLoginUser().getUser().getNickName())
            .setHandleTime(new Date())).collect(Collectors.toList());
        alarmHandleRecordService.saveBatch(resultList);
        return success();
    }

    @ApiOperation("获取告警视频")
    @PostMapping(value = "/getAlarmVideo")
    public AjaxResult getAlarmVideo(@Validated @RequestBody GetAlarmVideoParam param) {
        JSONObjectPro bigDataParam = new JSONObjectPro()
                .put("dvo", param.getDeviceId())
                .put("startTime", DateUtils.addSeconds(param.getStartTime(), -5))
                .put("endTime", DateUtils.addSeconds(param.getEndTime(), 15));
        BigDataResponse<JSONArray> response = HttpRequest.postJsonBigData(bigDataConfig.getApiUrl() + "/video/queryHisList", bigDataParam, JSONArray.class);
        if (!response.isSuccess()) throw new BigDataException();
        return success(response.getData());
    }
}
